<template>
  <div class="index">
    <div class="header">
      <div class="logo">
        <span class="iconfont iconnew"></span>
      </div>
      <div class="search">
        <div class="search-box" @click="$router.push('/search')">
          <span class="iconfont iconsearch"></span>
          <span>搜索新闻</span>
        </div>
      </div>
      <div class="user" @click="$router.push('/user')">
        <span class="iconfont iconwode"></span>
      </div>
    </div>
    <van-sticky class="more-sticky">
      <div class="more" @click="$router.push('/manage')">
        <span class="iconfont iconlianjie"></span>
      </div>
    </van-sticky>

    <van-tabs v-model="active" sticky animated swipeable>
      <!-- <template #nav-left> 左侧 </template> -->
      <van-tab :title="tab.name" v-for="tab in tabList" :key="tab.id">
        <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            :immediate-check="false"
            @load="onLoad"
          >
            <ht-post
              v-for="item in newsList"
              :key="item.id"
              :post="item"
            ></ht-post>
          </van-list>
        </van-pull-refresh>
      </van-tab>
      <!-- <template #nav-right> </template> -->
    </van-tabs>
  </div>
</template>

<script>
export default {
  name: 'index',
  methods: {
    onRefresh() {
      console.log(this.loading, '3333333333333333') // false
      this.newsList = []
      this.pageIndex = 1
      // this.loading = false
      this.loading = true
      this.finished = false
      /* 这个时候loading 默认是false  ，如果不改成true  会去吊函数 onload -- */
      setTimeout(() => {
        this.getNewsList(this.tabList[this.active].id)
      }, 1000)
    },
    onLoad() {
      console.log('上拉加载更多')
      // console.log(this.active + '2222')
      setTimeout(() => {
        this.pageIndex++
        this.getNewsList(this.tabList[this.active].id)
      }, 1000)
      console.log(this.pageIndex)
    },
    async getTabList() {
      const activeList = JSON.parse(localStorage.getItem('activeList'))
      if (activeList) {
        this.tabList = activeList
        this.getNewsList(this.tabList[0].id)
        return
      }
      const res = await this.$axios.get('/category')
      // console.log(res.data)
      const { statusCode, data } = res.data
      if (statusCode === 200) {
        this.tabList = data
        // console.log(this.tabList[0].id)
        // 发送请求 获取 第一个tab栏目的数据
        this.getNewsList(this.tabList[0].id)
      }
    },
    async getNewsList(id) {
      const res = await this.$axios.get('/post', {
        params: {
          category: id,
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
        },
      })
      const { statusCode, data } = res.data
      if (statusCode === 200) {
        // this.newsList = data
        this.newsList = [...this.newsList, ...data]
        console.log(this.newsList)
        // 加载完数据 将loading改为false
        this.loading = false
        this.refreshing = false
        if (data.length < this.pageSize) {
          this.finished = true
        }
      }

      // console.log(res.data)
    },
  },
  created() {
    this.getTabList()
  },
  data() {
    return {
      // 控制激活的下标
      active: 0,
      tabList: [],
      pageIndex: 1,
      pageSize: 6,
      newsList: [],
      loading: false,
      finished: false,
      refreshing: false,
    }
  },
  watch: {
    active(value) {
      //   console.log(value)
      this.newsList = []

      this.finished = false
      this.pageIndex = 1
      this.loading = true
      setTimeout(() => {
        // value是当前栏目的下标
        this.getNewsList(this.tabList[value].id)
      }, 1000)
    },
  },
}
</script>

<style lang="less" scoped>
.header {
  display: flex;
  height: 50px;
  background-color: #ff0000;
  line-height: 50px;
  text-align: center;
  color: #fff;
  .logo,
  .user {
    width: 70px;
  }
  .logo {
    span {
      font-size: 50px;
    }
  }
  .user {
    span {
      font-size: 24px;
    }
  }
  .search {
    flex: 1;
    .search-box {
      height: 34px;
      line-height: 34px;
      background-color: rgba(255, 255, 255, 0.5);
      margin-top: 8px;
      border-radius: 17px;
      font-size: 14px;
      span:first-child {
        margin-right: 5px;
      }
    }
  }
}

/deep/ .van-tabs__wrap {
  width: 85%;
}
.more {
  width: 15%;
  height: 44px;
  position: absolute;
  right: 0;
  z-index: 999;
  background-color: #fff;
  text-align: center;
  line-height: 44px;
}
.more-sticky {
  /deep/ .van-sticky--fixed {
    z-index: 100;
  }
}
</style>
